﻿using IP2Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Net.Security;

namespace IP2Server
{
    class User
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string AccessRights { get; set; }
        public string DoctorName { get; set; }
        public TcpClient TcpClient { get; set; }
        public IPAddress IPAddress { get; set; }
        public List<Measurement> Measurements { get; set; }
        public string UserFolder { get; private set; }
        public string UserDirectory { get; private set; }
        public string SessionFileName { get; private set; }
        public string SelectedUser { get; set; }
        public SslStream sslStream { get; set; }

        /// <summary>
        /// Constructor for the User class
        /// </summary>
        /// <param name="userName">The username of the User</param>
        /// <param name="password">The password of the User</param>
        /// <param name="accessRight">The accessrights of the User</param>
        /// <param name="doctorName">[Nullable] The name of the doctor</param>
        public User(string userName, string password, string accessRights, [Optional] string doctorName)
        {
            this.UserName = userName;
            this.Password = password;
            this.AccessRights = accessRights;

            if (doctorName != null)
                this.DoctorName = doctorName;
            else
                this.DoctorName = string.Empty;

            UserFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Patients\" + UserName + @"\";
            UserDirectory = DateTime.Today.ToShortDateString() + @"\";
        }

        /// <summary>
        /// Method for notifying the User that the thread has been started
        /// </summary>
        public void NotifyThreadCreated()
        {
            makeFolderAndSetFileName();
        }

        /// <summary>
        /// Method for making a user folder and setting the name of the sessionfile
        /// </summary>
        private void makeFolderAndSetFileName()
        {
            DirectoryInfo dirInfo = Directory.CreateDirectory(UserFolder + UserDirectory + @"\");

            int fileCount = dirInfo.GetFiles().Length;
            SessionFileName = "Session" + ++fileCount + ".dat";
        }

        public string getFullSessionPath()
        {
            return UserFolder + UserDirectory + SessionFileName;
        }
    }
}
